﻿Imports Mystery

Public Class EventsViewer


    Private _event_map As New Dictionary(Of Guid, SingleEventView)

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        AddHandler cd.contentAdded, AddressOf newEvent
        AddHandler cd.contentRemoved, AddressOf eventGone
    End Sub

    Public Sub eventGone(content As IContent)
        Me.Dispatcher.Invoke(Sub()
                                 If Not _event_map.ContainsKey(content.guid) Then Exit Sub
                                 Dim ev_view As SingleEventView = _event_map(content.guid)
                                 myStack.Children.Remove(ev_view)
                             End Sub)
        
    End Sub

    Public Sub newEvent(new_content As IContent)
        Me.Dispatcher.Invoke(Sub()
                                 If Not TypeOf (new_content) Is UserEvent Then Exit Sub
                                 Dim new_event As UserEvent = new_content
                                 Dim event_view As New SingleEventView(new_event)
                                 myStack.Children.Insert(0, event_view)
                                 _event_map(new_event.guid) = event_view
                             End Sub)
    End Sub

    Public Sub load()
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        Dim events As New List(Of UserEvent)(cd.getAll(Of UserEvent)())
        events.Sort(New UserEvent.Comparer)

        For Each ev As UserEvent In events
            Dim event_view As New SingleEventView(ev)
            myStack.Children.Add(event_view)
            _event_map(ev.guid) = event_view
        Next
    End Sub

    
    Private Sub deleteNormal_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bDelete.Click
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        Dim events As IEnumerable(Of UserEvent) = UserEvent.getAllNormalEvents
        For Each normal_event As UserEvent In events
            cd.Remove(normal_event)
        Next

    End Sub
End Class
